package atag.runtime.ln.nodeAttributes;

import java.io.IOException;
import javax.microedition.io.Datagram;

/**
 * Defines a static boolean attribute.
 *
 */
public class BooleanAttribute extends NodeAttribute {

	/**
	 * Creates a new instance of this attribute.
	 */
	public BooleanAttribute() {
	}

	/**
	 * Creates a new instance of this attribute.
	 *
	 * @param key the name of the attribute.
	 * @param value the value associated.
	 */
	public BooleanAttribute(String key, boolean value) {

		this.key = key;
		this.value = new Boolean(value);
	}

	public String toString() {

		return "BooleanAttribute key = " + key + ", value = " + value;
	}

    public Datagram serialize(Datagram dg) throws IOException {

        dg.writeByte(BOOL_ATTR_TYPE);
        super.serialize(dg);
        dg.writeUTF(key);
        dg.writeUTF(this.value.toString());
        return dg;
    }

     public static NodeAttribute deserialize(Datagram dg) throws IOException
     {
         String key = dg.readUTF();
         //dont know why this doesn't work
         //Boolean value = Boolean.valueOf(dg.readUTF());
         //Boolean value = Boolean.valueOf(dg.readUTF());
         Boolean value;
         String s = dg.readUTF();
         if (s.compareTo("true") == 0)
         {
             value = new Boolean(true);
         }
         else
         {
             value = new Boolean(false);
         }
         BooleanAttribute ba = new BooleanAttribute(key,value.booleanValue());
         return ba;
     }
}
